home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 05 Programming / BFRTH8.DOC < prev    next >
Text File  |  2019-04-13  |  19KB  |  335 lines

  1. *lm10:rm70
  2. *hd3:,Start Blazin' Forth,page -#-
  3. *ln2
  4. *cn1;FORWARD*cn0
  5. *ln2
  6.      By far the most read (and best) instructional book on Forth is STARTING FORTH, by Leo Brodie. It is very clear, amusing, and covers the territory. Unfortunately, since Starting Forth was written, Forth itself has undergone considerable evolution, so not everything in Starting Forth is applicable today. Additionally, Starting Forth is based on a particular dialect of Forth (POLY-FORTH), which is a Forth implementation by Forth Inc., the company Mr. Brodie worked for when he wrote his excellent book. As a result, even for the time it was written, certain passages and examples would not work on anything but a Forth Inc. system. ( .S is a classic example of this, but there are others. )
  7.      Blazin' Forth is written to support the Forth-83 standard. This is the most recent available standard, and the one which in my opinion, and from all I can tell, the opinion of most other Forth users, the most significant standard so far. As a result, it is not completely compatible with Starting Forth. ( Mr. Brodies latest book THINKING FORTH , uses Forth-83, by the way.)
  8.      Perhaps surprisingly, most of the examples Brodie gives work without a single change in Blazin' Forth. This document is intended to help you when the examples don't work as described, speeding up the learning process, easing the pain, and generally helping to prevent you from damaging yourself or your computer through frustation. Differences between the earlier Forth's and the present standard are described, and alternate definitions are provided.
  9.      Not all the comments are about differences - there are many more words in Blazin' Forth than are included in the Standard. I couldn't resist telling you about a few of them.
  10.      Good luck! And may the Forth be with you.
  11. *ln2
  12.                          SDB
  13.                          NY NY 1985
  14. *fp0
  15. *cn1;Chapter 1*cn0
  16.  
  17. Page 11
  18.  
  19. The definition STAR will work as supplied. Blazin' Forth contains a word called ASCII that can make words like STAR more readable (and save you a lot of time looking up characters in ASCII tables). Using ASCII , the definition of STAR would be:
  20.  
  21.    : STAR    ASCII *  EMIT ;
  22.  
  23. Try it - you'll like it.
  24.  
  25. By the way, if you change the definition of MARGIN on the next page to
  26.  
  27.    : MARGIN   CR  15 SPACES ;
  28.  
  29. your letter will look a little better on the screen.
  30. *ln2
  31. The Dictionary   Page-16
  32. *ln2
  33. Blazin' Forth normally allows 31 characters to be saved in a definitions name. (This is the number specified by the 83 standard.) However, should you prefer fewer characters to be saved, you can do this by changing the value of the variable WIDTH . For example, to have Blazin' Forth save only three characters of your definitions names, type 3 WIDTH !
  34. *ln2
  35. Say-What?
  36. *ln2
  37.   Regarding the second foot-note on page-18. The Forth-83 ." will not execute outside of a colon definition. Forth-83 provides the word .( for the occasions when you need to display strings outside of colon definitions. Use it like this:
  38.  
  39.    .( PRINT ME )
  40. *ln2
  41. *cn1;Chapter-2*cn0
  42. *ln2
  43. The handy hint on page-50 is a little notorious. I have, personally, never seen a system on which this definition would work. .S is never the less a very handy word, and Blazin' Forth contains it, already compiled into the system. Note that the extra 0 won't be there - you see only what's on the stack, or you get a message "STACK EMPTY", if nothing is on the stack.
  44. *ln2
  45. *cn1;Chapter-3*cn0
  46. *ln2
  47.     Blazin' Forth's editor is a "Starting-Forth-Clone". You will be able to follow this chapter pretty much as it stands. One very important point - you must issue the command MOUNT , to initialize the virtual memory system, before using any words that access the disk - like LIST , for example. Just remember to type MOUNT , and you will be OK . (You only have to give this command once - at the start of your editing session, unless you change disks, issue the command RESTART, or inadvertantly cause a disk error.)
  48.     Some general points: Brodie likes to keep his screen numbers high - in the 100's or 200's . Note that if you have a dual drive, such as a 4040 or MSD , you will have no problem with these high screen numbers ( of course, you will have to have a disk in drive 1!). However, if you are using the system with a single disk drive, such as a 1541, then the highest screen that can be accessed from that drive is 165 . Just keep all your screen numbers below this value, and you will be OK .
  49.      The WIPE command is very important, never edit a new screen without using this command first! It's so important, that Blazin Forth contains the word W , which is just a short way to perform a WIPE . Works just the same.
  50.      Finally, due to the characteristics of the 64's operating system, trailing blanks are suppressed, and not passed to the editor. This means, for example, that Brodies example of blanking a line:
  51.  
  52.    P bb <return>
  53.  
  54. won't work. Trailing blanks must be followed by an ^ character in order to be received by the editor. For example:
  55.  
  56.   P bb^ ( blanks current line )
  57.   U bb^ ( blanks line under current line)
  58.  
  59. That's enough for now - check back here when you get to FLUSH.
  60.  
  61. In Forth-83, FLUSH and SAVE-BUFFERS are not quite the same. Both save any updated blocks to the disk, but FLUSH will "hose down" the buffers - if you want to access the same blocks again, Forth will have to re-read them. SAVE-BUFFERS will leave all the blocks current - Forth won't have to re-read them if you need to access them again. Also, Blazin' Forth contains an additional comment word, // , which can be used to include 1 line comments in source screens.
  62. *ln2
  63. Getting Loaded    Page-81
  64. *ln2
  65. The suggestion here is not really very good. If the source for your text takes up more than one block, the best solution is not to use many load commands, as Brodie suggests, but to use THRU .
  66.      THRU takes two numbers on the stack. The first number is the first screen to load, and the second number is the last screen to load. THRU will then load these two screens, and all screens in between.
  67.      So, instead of putting 180 LOAD   181 LOAD  182 LOAD on your load screen, use:
  68.  
  69. 180 182  THRU
  70.  
  71. instead. It's really much better.
  72. *ln3
  73. *cn1;Chapter 4*cn0
  74. *ln3
  75. A Closer Look at IF . Page-95
  76.  
  77.   One of the big differences between the 83 standard and earlier Forths is that Forth-83 returns a negative 1 ( -1 ) instead of 1 when the result of a test like 0= or 0< is true. So Brodies examples will look like this:
  78.  
  79. 5 4 > . -1  OK
  80. 5 4 < . 0   OK
  81.  
  82. NOT works differently too. In earlier Forths, it was really just another name for 0= . But in Forth-83, it returns the ones-compliment of the number on the top of the stack. Notice that the '83 NOT still reverses the value of the '83 flags:*ln2
  83. -1 NOT . 0 OK
  84. 0 NOT . -1 OK
  85. *ln2
  86. But NOT won't change any non-zero value to false ( 0 ) like the older NOT would:*ln2
  87. 1 NOT . -2  OK
  88. *ln2
  89. And, of course, FORTH will still regard -2 as being TRUE , since it isn't zero.
  90.  
  91. So when upgrading programs, or reading books that talk about earlier Forth systems, keep this in mind. On the other hand, if you're not quite sure what all this means, and want to play it safe, just replace every pre-83 NOT with 0= , and you'll be OK .
  92. *ln2
  93. A Little Logic    Page-97
  94. *ln2
  95. To upgrade these examples to FORTH-83, just replace every occurance of 1 with -1 , and you'll have a Forth-83 example. While we are on the subject, Blazin' Forth contains the two flags pre-compiled as constants. TRUE leaves a true value (-1) on the top of the stack, and FALSE leaves a false value ( 0 ). You'll appreciate this more when you learn about constants later in the book.
  96. *ln2
  97. Two Words with built in IFs  Page 102
  98. *ln2
  99. Just a note - if you do want to add stack checking to your application, all you need to add in Blazin' Forth is the word ?STACK . The ABORT" isn't needed, since ?STACK itself contains the necessary ABORT" .
  100. *ln2
  101. Problems   Page-104
  102. *ln2
  103. Problem 5
  104. Actually, in Forth-83, 0 STARS won't print just one star, but 65,535 stars. Brodies answer ( or the one you cook up for yourself) should fix this problem, but there are better ways. More on this when we get to the chapter on LOOPs. Incidentally, if you tried 0 STARS before reading this note, you can get control of things by hitting RUN/STOP RESTORE , just like in BASIC .
  105. *ln2
  106. Chapter-5
  107.  
  108. The Return Stack
  109.  
  110. Page-110
  111.  
  112. The definitions for I , I' and J are not correct for Forth-83. I' does not exist, and I and J are both defined differently. Don't worry, they still do what they are supposed to (which is return the current index value of a loop), it's just that they don't do what Brodie says they would. Just wait till chapter 6 for more information on these two words.
  113.  
  114. The correct word to copy the top of the return stack is R@ in Forth-83. (i.e. R@ does what Brodie says I should do.)
  115.  
  116. Page-111
  117.  
  118. The phrase >R SWAP I would be >R SWAP R@  in Forth - 83. Both phrases will crash the system
  119.  
  120. Page-112
  121.  
  122. The definition of QUADRATIC should be:
  123.  
  124.    : QUADRATIC ( A B C X -- N )
  125.          >R SWAP ROT R@ * + R> * + ;
  126.  
  127. In other words, R@ should be used instead of I . See the note above for an explanation.
  128.  
  129. Page-119
  130.  
  131. The footnote on this page contains a typo in my copy. The correct definition is:*ln2
  132.      : R%   50 */  1+  2/ ;
  133. *ln2
  134. Problems
  135.  
  136. Problem 1
  137.  
  138. In the answer to this problem, Brodie means NEGATE , not MINUS .
  139.  
  140. You might also notice that Blazin' Forth does not return a -17 like Brodie says it should. This is due to the 83-standards use of floored division. In floored division, numbers are always truncated to the next lower value. To get the same result as Brodie in 83-Forth systems, use ABS and then NEGATE.
  141. *ln2
  142. *cn1;Chapter 6*cn0
  143. *ln2
  144. Forth-83 uses a new loop structure which is very fast, but differs in a few points from the older loops used in older Forths. Sorry about this, but there will be a little more information here than previously.
  145. *ln2
  146. Page-130
  147. *ln2
  148. Earlier (in the notes to chapter 5) we said that "I" works differently in '83 Forths. Well, originally, "I" was meant to be used as it is here, to access the loop index. Since in those days, the loop index was stored on the top of the return stack, you could also use "I" to copy that value, even if you weren't in a loop, which was not a purpose for which it was originally intended. So that's the difference, I now only returns the loop index, while R@ copies the top of the return stack to the parameter stack. (For the curious: In Blazin' Forth, I returns the sum of the top two return stack items. It may seem strange, but it works just fine!)
  149.  
  150. The terminating condition for loops is also different than in earlier Forths. Don't be fooled by the fact that most of the examples in this chapter work the same - the exit condition for the loops are different, and if you ignore this fact, you are going to get bit by it.
  151.  
  152. In '83 loops, the loop is finished when the Index crosses the boundary between the LIMIT and LIMIT - 1 . A couple of examples:
  153.  
  154. If the Index is 0 , and the LIMIT is 10 , then the loop will terminate when the Index is incremented from 9 to 10 (crosses the boundary).
  155.  
  156. If the Index is 0 , and the Limit is -10 , then the loop will terminate when the index is incremented from -11 to -10 or decremented from -10 to -11 . This means, if you are using a DO..LOOP, that your loop will take the long way around...0 1 2 3 4 ...32767 -32768 .....-15 -14 -13 -12 -11 . If you are using +LOOP with a negative step value, then your loop will look like this: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 (boundary crossed, and looped terminated).
  157. *ln2
  158. One odd side effect of this new loop structure is that anytime the limit and the index are equal, the loop will execute 65,535 times. So, for example, the word STARS :
  159.  
  160. : STARS   0 DO ASCII * EMIT LOOP ;
  161.  
  162. If used like this:
  163.  
  164.   0 STARS
  165.  
  166. will EMIT an awful lot of stars. Blazin' Forth contains a word to handle cases like this - ?DO . ?DO acts just like DO , except when the loop arguments are equal. If the arguments are equal, then the loop won't be entered at all - FORTH will jump to the word following LOOP or +LOOP . In addition to keeping out nasty surprises (like 0 STAR) ?DO can also shorten and speed up your code. For example, we often find we don't want to perform a loop when the LIMIT is zero (as in STARS , above.) The traditional way to do this is:
  167.  
  168. : STARS   ?DUP IF 0 DO ASCII * EMIT LOOP THEN ;
  169.  
  170. But you can now do it this way:
  171.  
  172. : STARS   0 ?DO IF ASCII * EMIT LOOP ;
  173.  
  174. You can think of ?DO as having a built in IF .
  175. *fp0
  176. Page 135
  177.  
  178. The definition given by Brodie:
  179.  
  180. : TEST 100 10 DO I . -1 +LOOP ;
  181.  
  182. will execute quite a few more times than one (in '83 Forth). Remember, '83 loops stop when the boundary is crossed between the LIMIT and LIMIT-1 . This crossing can be in either direction, either down ( 101 100 (crossed to 99, so exit) ) or up ( 98 99 (crossed to 100, so exit)) . Since TEST counts down, it will proceed all the way around the number circle, starting with 10, and then going negative, until it eventually goes 101 100 (as in our first example, above). Just though I would mention this.
  183. *ln2
  184. Page-140 LEAVE
  185. *ln2
  186. In '83 Forth, LEAVE leaves immediately, not at the next execution of LOOP or +LOOP. Brodies examples will work the same for both '83 and earlier Forths, but problems can arise if you aren't aware of the difference. For example:
  187. *ln2
  188. : FOO   0 DO   I DUP 100 = IF LEAVE THEN . LOOP ;
  189.  
  190. FOO is a word that will exit when the loop counter reaches 100 . Notice, however, that the actual behaviour of FOO will differ between '83 and earlier standards. In earlier Forths, FOO's execution would be this:
  191.  
  192. When the Counter reaches 100, LEAVE will set the index equal to the limit.
  193.  
  194. " . " will be executed, printing 100 on the terminal screen, and leaving the stack empty.
  195.  
  196. LOOP will execute, and discover its time to stop (since LEAVE changed the INDEX).
  197.  
  198. In '83 Forth, this is what happens.
  199.  
  200. When the Counter reaches 100, LEAVE will jump to the word past LOOP , which in this case is the end of the definition. 100 will not be printed, but left on the stack. Quite different behaviours result, as you can see. Just remember that the 83 LEAVE leaps , and you'll be OK .
  201.  
  202. By the way, Blazin' Forth has a handy word ?LEAVE . ?LEAVE will cause an exit from the loop if the flag on top of the stack is true, otherwise, the loop continues undisturbed. This can shorten your code quite a bit. Here's an example:
  203.  
  204. : BAR   0 DO I 100 = IF LEAVE THEN LOOP ;
  205.  
  206. This word just leaves when the index reaches 100. Using ?LEAVE would make things a little nicer:
  207.  
  208. : BAR   0 DO I 100 = ?LEAVE LOOP ;
  209.  
  210. Works just the same. Just remember that you have to leave a flag on the stack for ?LEAVE to eat.
  211. *ln2
  212. *cn1;Chapter 7*cn0
  213. *ln2
  214. Section II
  215.  
  216. Page 161
  217.  
  218. The Forth-83 word for U* is UM* . It works the same as U* , but has been renamed.
  219.  
  220. The Forth-83 word for U/MOD is UM/MOD . It works the same.
  221. *ln2
  222. /LOOP is not included. It is no longer necessary in '83 systems. (For the curious: /LOOP was essentially a kludge to fix a problem with the older loops. Just use +LOOP instead.)
  223. *ln2
  224. Page 164
  225. *ln2
  226. '83 systems will only accept the period as punctuation in numbers. However, in Blazin' Forth, you may change this. Here are the steps to follow to get Blazin' Forth to accept the same characters those given by Brodie:
  227.  
  228. First, type in and LOAD the following definition:
  229.  
  230. : NEW-PUNCT   ( CHAR -- FLAG)
  231.      ASCII : OVER = SWAP
  232.      ASCII + ASCII 0 >R OVER < SWAP R> < AND OR ;
  233.  
  234. Then, once your NEW-PUNCT has been loaded, type the following:
  235.  
  236. ' NEW-PUNCT IS PUNCT?
  237.  
  238. Type the above carefully, or you could cause problems (don't forget the ' ). Blazin' Forth will now accept numbers as described in this section. If you prefer this new version, you may save the system with the word SAVE-FORTH. If you want to change back to the oolder version, do the following:
  239.  
  240. ' (PUNCT?) IS PUNCT?
  241. *ln2
  242. Number Formatting -- Double Length Unsigned.
  243.  
  244. Pages 167-168
  245.  
  246. All of the definitions in this section will work as described, but they would be so much more readable using ASCII . Here are some examples:
  247.  
  248. : .PH#   <#  # # # # ASCII - HOLD  #S  #> TYPE SPACE ;
  249.  
  250. : .DATE  <#  # # ASCII / HOLD # # ASCII / HOLD  #S  #> TYPE SPACE ;
  251.  
  252. : :00   #  SEXTAL # DECIMAL ASCII : HOLD ;
  253. *ln2
  254. Page 171
  255.  
  256. The definition that Brodie gives for .$ won't work in 83-forth. SIGN works on the value on the top of the stack. A definition that will work in 83-Forth is:
  257.  
  258. : .$   SWAP OVER DABS
  259.        <# # # ASCII . HOLD #S ROT ( to bring signed number to top)
  260.        ASCII $ HOLD SIGN #> TYPE SPACE ;
  261. You might also noticw that the positions of the $ and SIGN have been switched. I prefer my negative dollars to have their signs in front of the $ sign. That's the only reason for this change.
  262. *ln2
  263. Page 174
  264.  
  265. The warning to experimenters is not true in Blazin' Forth. You can include punctuated, double-precision numbers inside of colon definitions, and they will work just fine. (I have never understood this restriction.) Note that other systems may not allow this.
  266. *ln2
  267. Page 175
  268.  
  269. The definition for R% on this page is simply wrong. It should be, instead:
  270.  
  271. : R%   10 M*/ 5 M+ 10 M/ S>D ROT DROP ;
  272.  
  273. Which gives $148.15 as an answer.
  274.  
  275. *fp0
  276. *cn1;Chapter 8*cn0
  277. *ln2
  278. Page 194
  279.  
  280. See the note above (page 174). You do not need to use 2CONSTANT to include double numbers in Blazin' Forth. You may wish to do so as a matter of style, but it is not forced on you.
  281. *ln2
  282. *cn1;Chapter 9*cn0
  283. *ln2
  284. Brodies example:
  285.  
  286. 110 ' LIMIT !
  287.  
  288. won't work with the '83 tick. Use
  289.  
  290. 110 ' LIMIT >BODY !
  291.  
  292. instead. By the way, LIMIT is an important system constant in Blazin' Forth, and I don't recommend that you try this example on it.
  293. *ln2
  294. Vectored Execution:
  295. *ln2
  296. Page 218
  297.  
  298. The behaviour of ' and ['] in '83 Forth is exactly that described here. Ignore the footnote on '79s tick. You might also wish to look at the documentation for DEFER IS , which are very handy words for handling Vectored Execution.
  299. *ln2
  300. The Structure of a Dictionary Entry.
  301. *ln2
  302. Page 220
  303.  
  304. The structure of a dictionary entry for Blazin' Forth than described here. (Note: The '83 standard regards this as a system dependency, and does not specify any particular form of dictionary structure. Other systems may, and probably do, differ.)
  305.  
  306. In Blazin' Forth, the structure is:
  307.  
  308.      LINK FIELD
  309.      NAME FIELD
  310.      CODE POINTER FIELD
  311.      PARAMETER FIELD
  312.  
  313. There is also an optional field, called the VIEW or LOCATE field. This field is used by LOCATE, and is added or not at the option of the user. With the view field included, the structure looks like this:
  314.  
  315.      VIEW FIELD
  316.      NAME FIELD
  317.      CODE POINTER FIELD
  318.      PARAMETER FIELD
  319.  
  320. Compiling without the VIEW FIELD results in more compact (but not faster running) definitions, but note that LOCATE will not operate on any definition without a VIEW FIELD. To prevent the view field from being compiled:
  321.  
  322. VIEW OFF
  323.  
  324. To re-enable the compilation of the VIEW FIELD:
  325.  
  326. 2 VIEW !
  327. *ln2
  328. Parameter Field
  329. *ln2
  330. Page 223
  331.  
  332. The address returned by tick and expected by EXECUTE is not the parameter field address, but the CODE FIELD address. If you want the parameter field address, you may convert the address returned by tick (the code field address) to the parameter field address using the '83 standard word >BODY . For an illustration of its use, see the example using LIMIT , given earlier.
  333. *fp0
  334. *lk:starting2
  335.